<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #canvas {
            border: 1px solid black;
        }
    </style>
</head>
<body>
<canvas width="600px" height="300px" id="canvas"></canvas>
<script>
    const canvas = document.querySelector("#canvas")
    const ctx = canvas.getContext("2d")
    ctx.moveTo(50, 50)
    ctx.lineTo(50, 230)
    ctx.strokeStyle = 'red'
    ctx.lineWidth = 3
    ctx.stroke()

    // 开启一个独立的绘图作用域，区分颜色
    // 上面的颜色会影响到下面，但下面不会影响到上面
    ctx.beginPath()
    ctx.moveTo(50, 230)
    ctx.lineTo(450, 230)
    ctx.strokeStyle = 'yellow'
    ctx.lineWidth = 3
    ctx.stroke()

    ctx.beginPath()
    ctx.moveTo(450, 230)
    ctx.lineTo(450, 50)
    ctx.strokeStyle = 'blue'
    ctx.lineWidth = 3
    ctx.stroke()

    ctx.beginPath()
    ctx.moveTo(450, 50)
    ctx.lineTo(50, 50)
    ctx.strokeStyle = 'green'
    ctx.lineWidth = 3
    ctx.stroke()

</script>
</body>
</html>